﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FeatureModelTool.FeatureModelGraphical;
using FeatureModelCore;
using FeatureModelTool.Windows;
using FeatureModelCore.FMAnalyzer;
using FeatureModelCore.Configuration;
using System.Xml.Linq;
using System.IO;
using FeatureModelCore.Optimized;
using FeatureModelCore.Layout;

namespace FeatureModelTool.FileView
{
    /// <summary>
    /// Interaction logic for FeatureModelGraphicalTab.xaml
    /// </summary>
    public partial class FeatureModelGraphicalTab : TabItem, IMainTab
    {
        public bool Dirty { get { return GraphicalView.TreeInstance.Dirty; } }
        public FeatureModelGraphicalView GraphicalView { get { return this.GraphicView; } }
        public FeatureModelGraphicalTab()
        {
            InitializeComponent();

            #region Menu Context
            MenuItem createCons = new MenuItem()
            {
                Name = "CreateConstraint",
                Header = "New Cross Tree Constraint"
            };
            createCons.Click += new RoutedEventHandler(createConstraint_EventHandler);

            MenuItem save = new MenuItem()
            {
                Name = "save",
                Header = "Save"
            };
            save.Click += new RoutedEventHandler(save_EventHandler);

            MenuItem close = new MenuItem()
            {
                Name = "close",
                Header = "Close"
            };
            close.Click += new RoutedEventHandler(close_EventHandler);

            MenuItem setLayout = new MenuItem()
            {
                Name="SetLayout",
                Header = "Set Layout"
            };
            setLayout.Click += new RoutedEventHandler(setLayout_EventHandler);

            MenuItem analyze = new MenuItem()
            {
                Name = "Analyze",
                Header = "Analyze"
            };

            //MenuItem valid = new MenuItem()
            //{
            //    Name = "Valid",
            //    Header = "Valid Feature Model"
            //};
            //valid.Click += new RoutedEventHandler(valid_EventHandler);
            MenuItem allProduct = new MenuItem()
            {
                Name = "AllProduct",
                Header = "All Products"
            };
            allProduct.Click += new RoutedEventHandler(allProduct_EventHandler);
            MenuItem generateOptimizedFM = new MenuItem()
            {
                Name = "generateOptimizedFM",
                Header = "Generate Optimized Feature Model Tree"
            };
            generateOptimizedFM.Click += new RoutedEventHandler(generateOptimizedFM_EventHandler);
            MenuItem generateConfig = new MenuItem()
            {
                Name = "generateConfig",
                Header = "Generate Config"
            };
            generateConfig.Click += new RoutedEventHandler(generateConfig_EventHandler);

            //analyze.Items.Add(valid);
            analyze.Items.Add(allProduct);
            analyze.Items.Add(generateOptimizedFM);
            analyze.Items.Add(generateConfig);


            this.ContextMenu = new ContextMenu();
            this.ContextMenu.Items.Add(createCons);
            this.ContextMenu.Items.Add(new Separator());
            this.ContextMenu.Items.Add(save);
            this.ContextMenu.Items.Add(close);
            this.ContextMenu.Items.Add(new Separator());
            this.ContextMenu.Items.Add(setLayout);
            this.ContextMenu.Items.Add(analyze);
            #endregion

        }

        public void TabItem_SizeChanged()
        {
            FileViewer fileViewer = this.Parent as FileViewer;
            if (fileViewer == null)
            {
                return;
            }

            double width = fileViewer.ActualWidth - 10;
            double height = fileViewer.ActualHeight - 32;

            this.GraphicalView.Height = height;
            this.GraphicalView.Width = width;
        }

        #region Context Menu Event Handler
        /// <summary>
        /// Set Layout
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void setLayout_EventHandler(object sender, RoutedEventArgs e)
        {
            TreeLayout.SetFeatureModelLayout(this.GraphicalView.TreeInstance);
            this.GraphicalView.RefreshPosition(null, null);
        }
        /// <summary>
        /// save
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void save_EventHandler(object sender, RoutedEventArgs e)
        {
            this.SaveInstance();
        }
        /// <summary>
        /// 
        /// </summary>
        public void SaveInstance()
        {
            ((FeatureModelTree)this.GraphicalView.TreeInstance).Save();
        }
        /// <summary>
        /// close
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void close_EventHandler(object sender, RoutedEventArgs e)
        {
            if (this.Dirty)
            {
                ClosingWarningDialog dialog = new ClosingWarningDialog();
                dialog.ShowDialog();
                if (dialog.ToSave == true)
                {
                    this.SaveInstance();
                }
                if (dialog.ToClose == false)
                {
                    return;
                }
            }

            TabControl tabControl = (TabControl)this.Parent;
            tabControl.Items.Remove(this);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //protected void valid_EventHandler(object sender, RoutedEventArgs e)
        //{
        //    FeatureModelTree fmTree = (FeatureModelTree)this.GraphicalView.TreeInstance;
        //    bool validIntegrity = fmTree.ValidFeatureModelIntegrity();
        //    bool validOptimizedTree = fmTree.Analyzer.OptimizedTree.ValidAllCrossTreeConstraint();
        //    if (validIntegrity && validOptimizedTree)
        //    {
        //        return;
        //    }
        //    MessageBox.Show("Invalid Feature Model");
        //}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void createConstraint_EventHandler(object sender, RoutedEventArgs e)
        {
            //FeatureModelTree fmTree = (FeatureModelTree)this.GraphicalView.TreeInstance;
            //NewConstraintsDialog dia = new NewConstraintsDialog(fmTree.AllNodes);
            //dia.ShowDialog();
            //if (dia.Dirty == false)
            //{
            //    return;
            //}

            //fmTree.CreateConstraint(dia.NodeA.ID, dia.NodeB.ID, dia.ConstraintType);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void allProduct_EventHandler(object sender, RoutedEventArgs e)
        {
            FeatureModelTree fmTree = (FeatureModelTree)this.GraphicalView.TreeInstance;
            FMProductCollection allProducts = fmTree.AllProducts();

            AllProductsWindows dialog = new AllProductsWindows(allProducts);
            dialog.Show();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void generateConfig_EventHandler(object sender, RoutedEventArgs e)
        {
            FeatureModelTree fmTree = (FeatureModelTree)this.GraphicalView.TreeInstance;
            ConfigFeatureModel configFm = new ConfigFeatureModel(fmTree);
            XDocument xdoc = configFm.ToXDoc();
            string fileFullPath = fmTree.FolderPath + "\\" + fmTree.Root.Name + ".config";
            using (StreamWriter writer = File.CreateText(fileFullPath))
            {
                writer.Write(xdoc);
            }

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void generateOptimizedFM_EventHandler(object sender, RoutedEventArgs e)
        {
            //FeatureModelTree fmTree = (FeatureModelTree)this.GraphicalView.TreeInstance;
            //OptimizedFeatureModel optimizedTree = fmTree.Analyzer.OptimizedTree;
            //FeatureModelTree optimizedFm = optimizedTree.ToFeatureModelTree();
            //string fileFullPath = fmTree.FolderPath + "\\optimized_" + fmTree.Root.Name + ".fm";
            //optimizedFm.SaveAs(fileFullPath);

            //MessageBox.Show("Generate Optimized Feature Model Complete.");
        }
        #endregion

    }
}
